package com.xydtech.workflow.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.workflow.domain.WflFlowdefine;
import com.xydtech.workflow.domain.WflFlowversion;
import com.xydtech.workflow.mapper.WflFlowdefineMapper;
import com.xydtech.workflow.service.IWflFlowdefineService;
import com.xydtech.workflow.service.IWflFlowversionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程定义Service业务层处理
 *
 * @author JHON
 * @date 2021-04-11
 */
@Service
public class WflFlowdefineServiceImpl implements IWflFlowdefineService {
    @Autowired
    private WflFlowdefineMapper wflFlowdefineMapper;
    @Autowired
    private IWflFlowversionService versionService;

    /**
     * 查询流程定义
     *
     * @param flowId 流程定义ID
     * @return 流程定义
     */
    @Override
    public WflFlowdefine selectWflFlowdefineById(Long flowId) {
        return wflFlowdefineMapper.selectWflFlowdefineById(flowId);
    }

    /**
     *根据流程流程类型和法人机构查询流程信息
     * @Title: selectWflFlowdefineByFlowType
     * @param wflFlowdefine
     * @return java.util.List<com.xydtech.workflow.domain.WflFlowdefine>
     * @exception
     * @author JHON
     * @Date 2022/5/11 16:52
     **/
    @Override
    public List<WflFlowdefine> selectFlowdefineByFlowType(WflFlowdefine wflFlowdefine) {
        return wflFlowdefineMapper.selectFlowdefineByFlowType(wflFlowdefine);
    }

    /**
     * 查询流程定义列表
     *
     * @param wflFlowdefine 流程定义
     * @return 流程定义
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<WflFlowdefine> selectWflFlowdefineList(WflFlowdefine wflFlowdefine) {
        return wflFlowdefineMapper.selectWflFlowdefineList(wflFlowdefine);
    }

    /**
     * 新增流程定义
     *
     * @param wflFlowdefine 流程定义
     * @return 结果
     */
    @Override
    public int insertWflFlowdefine(WflFlowdefine wflFlowdefine) {
        wflFlowdefine.setCreateTime(DateUtils.getNowDate());
        return wflFlowdefineMapper.insertWflFlowdefine(wflFlowdefine);
    }

    /**
     * 复制流程定义
     *
     * @param wflFlowdefine 流程定义
     * @return 结果
     */
    @Override
    public int copyWflFlowdefine(WflFlowdefine wflFlowdefine) {
        //复制流程定义
        wflFlowdefineMapper.copyWflFlowdefine(wflFlowdefine);
        //复制流程启用的版本信息
        WflFlowversion wflFlowversion = new WflFlowversion();
        wflFlowversion.setVersionId(wflFlowdefine.getCurreVersion());
        wflFlowversion.setFlowId(wflFlowdefine.getFlowId());
        return versionService.copyWflFlowVersion(wflFlowversion);
    }

    /**
     * 修改流程定义
     *
     * @param wflFlowdefine 流程定义
     * @return 结果
     */
    @Override
    public int updateWflFlowdefine(WflFlowdefine wflFlowdefine) {
        return wflFlowdefineMapper.updateWflFlowdefine(wflFlowdefine);
    }

    /**
     * 删除流程定义对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWflFlowdefineByIds(String ids) {
        return wflFlowdefineMapper.deleteWflFlowdefineByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程定义信息
     *
     * @param flowId 流程定义ID
     * @return 结果
     */
    @Override
    public int deleteWflFlowdefineById(Long flowId) {
        return wflFlowdefineMapper.deleteWflFlowdefineById(flowId);
    }
}
